package com.example.smokealarm.controller;

import com.example.smokealarm.model.Note;
import com.example.smokealarm.repository.NoteRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/api/person")
public class NoteController {

    private final NoteRepository noteRepository;

    public NoteController(NoteRepository noteRepository) {
        this.noteRepository = noteRepository;
    }
/*    @GetMapping
    public List<Note> getAllNotes() {
        return noteRepository.findAll();
    }*/
    @GetMapping("/notes")
    public List<Note> getAllNotes(@RequestParam String userId) {
        return noteRepository.findByUserId(userId);
    }

    @PostMapping("/notes")
    public Note addNote(@RequestBody Note note, @RequestParam String userId) {
        note.setUserId(userId); // 设置 userId 到 note 对象
        note.setCreatedTime(new Timestamp(System.currentTimeMillis())); // 设置创建时间
        note.setUpdatedTime(new Timestamp(System.currentTimeMillis())); // 设置更新时间
        return noteRepository.save(note); // 保存 note 对象
    }

}